package org.meetunexpectedly.controller;

import org.meetunexpectedly.constant.RedisConstant;
import org.meetunexpectedly.entity.vo.Result;
import org.meetunexpectedly.entity.vo.Captcha;
import org.meetunexpectedly.entity.user.User;
import org.meetunexpectedly.entity.vo.ForgotPwdVO;
import org.meetunexpectedly.entity.vo.RegisterVO;
import org.meetunexpectedly.holder.IPHolder;
import org.meetunexpectedly.limit.RequestLimit;
import org.meetunexpectedly.service.login.LoginService;
import org.meetunexpectedly.util.JwtUtils;
import org.meetunexpectedly.util.RedisCacheUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description: 登录控制层
 * @Author: pupil
 * @Date: 2024/08/03 下午 9:07
 */
@RestController
@RequestMapping("/meetunexpectedly/login")
public class LoginController {

    @Autowired
    private LoginService loginService;

    @Autowired
    private RedisCacheUtil redisCacheUtil;

    /**
     * 登录
     *
     * @param user 用户
     * @return
     */
    @PostMapping
    public Result login(@RequestBody @Validated User user, HttpServletRequest request) {
        IPHolder.set(request);
        user = loginService.login(user);
        user.setPassword("保护用户账号安全");
        // 用户上线(优化前)
        // UserOnlineHolder.userOnline(userVO.getId());
        // 用户上线(优化后)
        redisCacheUtil.set(RedisConstant.USER_ONLINE + user.getId(), user.getId(), RedisConstant.USER_ONLINE_TIME);
        // 登录成功，生成token
        String token = JwtUtils.getJwtToken(user.getId(), user.getNickName());
        Map<Object, Object> map = new HashMap<>();
        map.put("token", token);
        map.put("nickName", user.getNickName());
        map.put("user", user);
        return Result.ok().data(map);
    }

    /**
     * 获取图形验证码
     *
     * @param response 网络请求
     * @param uuId     uuId
     */
    @GetMapping("/captcha.jpg/{uuId}")
    public void getCaptcha(HttpServletResponse response, @PathVariable String uuId) throws IOException {
        loginService.getCaptcha(response, uuId);
    }

    /**
     * 获取邮箱验证码
     *
     * @param captcha 图形验证码
     * @return
     */
    @PostMapping("/getCode")
    public Result getCode(@RequestBody @Validated Captcha captcha) throws Exception {
        if (!loginService.getCode(captcha)) {
            return Result.error().message("验证码错误");
        }
        return Result.ok().message("发送成功，请耐心等待！");
    }


    /**
     * 检测邮箱验证码
     *
     * @param email 邮箱
     * @param code  邮箱验证码
     * @return
     */
    @PostMapping("/check")
    public Result checkCode(String email, Integer code) {
        loginService.checkCode(email, code);
        return Result.ok().message("验证成功");
    }


    /**
     * 注册
     *
     * @param registerVO 注册信息
     * @return
     * @throws Exception
     */
    @PostMapping("/register")
    public Result register(@RequestBody @Validated RegisterVO registerVO) throws Exception {
        if (!loginService.register(registerVO)) {
            return Result.error().message("注册失败,验证码错误");
        }
        return Result.ok().message("注册成功");
    }

    /**
     * 忘记密码
     *
     * @param forgotPWD 密码重置信息
     * @return
     */
    @PostMapping("/forgotPassword")
    public Result forgotPassword(@RequestBody @Validated ForgotPwdVO forgotPWD) {
        if (!loginService.forgotPassword(forgotPWD)) {
            return Result.error().message("密码重置失败,验证码错误");
        }
        return Result.ok().message("密码重置成功");
    }

}
